package logic

import (
	"context"

	"go-zero-mall/app/admin/cmd/rpc/internal/svc"
	"go-zero-mall/app/admin/cmd/rpc/pd"
	"go-zero-mall/app/admin/model"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type AdminInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAdminInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminInfoLogic {
	return &AdminInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理员-信息
func (l *AdminInfoLogic) AdminInfo(in *pd.AdminInfoReq) (*pd.AdminInfoResp, error) {
	d, err := l.svcCtx.UmsAdminModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}
	var d1 model.UmsAdmin
	copier.Copy(&d1, d)
	m, err := l.svcCtx.UmsAdminModel.FindMenus(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}
	r, err := l.svcCtx.UmsAdminModel.FindRoles(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}

	return &pd.AdminInfoResp{
		Username: d1.Username,
		Roles:    r,
		Icon:     d1.Icon,
		Menus:    m,
	}, nil
}
